package com.sendi.mobilework.pager.ext;

import android.app.Activity;
import android.content.Intent;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.sendi.mobilework.AddApplyActivity;
import com.sendi.mobilework.R;
import com.sendi.mobilework.api.Api;
import com.sendi.mobilework.datamodel.Workflow;
import com.sendi.mobilework.datamodel.WorkflowWorker;
import com.sendi.mobilework.global.GlobalContent;
import com.sendi.mobilework.pager.BasePager;
import com.sendi.mobilework.util.CacheUtils;
import com.sendi.mobilework.util.HttpUtils;
import com.sendi.mobilework.view.RefreshListView;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

/**
 * Created by Administrator on 2017/4/5.
 */

public class WorkflowWorkerPager extends BasePager implements View.OnClickListener{

    private RefreshListView mListView;
    private List<Workflow.RecordBean>mList;
    private WorkflowWorkerAdapter mAdapter;
    private final String ACTION="findRota";
    private String TAG="SENDI";

    private Api mApi;

    public WorkflowWorkerPager(Activity activity) {
        super(activity);
        Retrofit retrofit = HttpUtils.getRetrofit(GlobalContent.WORKERFLOW_MANAGER);
        mApi = retrofit.create(Api.class);
    }

    @Override
    public void initView() {
        super.initView();
        mTitleText.setText("工作流程");
        mImageButton.setVisibility(View.VISIBLE);
        mImageButton.setOnClickListener(this);

        View view=View.inflate(mActivity,R.layout.workflow_worker_layout,null);
        mListView= (RefreshListView) view.findViewById(R.id.workflow_worker_listview);

        mListView.setOnRefreshListener(new RefreshListView.OnRefreshListener() {
            @Override
            public void onRefresh() {//刷新数据
                getDatafromServelet();
            }
        });


        mFrameLayout.addView(view);
    }

    @Override
    public void initData() {
        mList=new ArrayList<>();
        mAdapter=new WorkflowWorkerAdapter();
        String cache= CacheUtils.getCache(mActivity, GlobalContent.WORKERFLOW_MANAGER+ACTION);
        if (!TextUtils.isEmpty(cache)){//获取缓存数据
//            mWorkflowList=cache;
            Gson gson=new Gson();
            mList=gson.fromJson(cache, Workflow.class).getRecord();
            Log.i(TAG, "initData: "+mList.get(1).apply_time);
            if (mList!=null){
                mListView.setAdapter(mAdapter);
            }
        }
            getDatafromServelet();

        mListView.setAdapter(mAdapter);
    }


    /**
     * 从网络获取数据
     */
    public void getDatafromServelet() {
        mApi.getWorkflowManager().enqueue(new Callback<Workflow>() {
            @Override
            public void onResponse(Response<Workflow> response) {
                mList = response.body().getRecord();
                Gson gson=new Gson();
                Log.i("SENDI", "onResponse: " + mList.get(1).getStatus());
                Log.i("SENDI", "onResponse: " + mList.get(1).getWorker_name());
                Log.i("SENDI", "onResponse: " + mList.get(1).apply_time);
                Log.i("SENDI", "onResponse: "+gson.toJson(response.body()));
                if (response.isSuccess()){
                    //缓存数据
                    CacheUtils.setCache(mActivity,GlobalContent.WORKERFLOW_MANAGER+ACTION,
                            gson.toJson(response.body()));
                    mListView.setAdapter(mAdapter);
                    mListView.refreshComplete(true);
                }
            }

            @Override
            public void onFailure(Throwable t) {
                Log.i(TAG, "onFailure: "+t.toString());
                mListView.refreshComplete(false);
            }
        });
    }


    class WorkflowWorkerAdapter extends BaseAdapter{
        @Override
        public int getCount() {
            return mList.size();
        }

        @Override
        public Workflow.RecordBean getItem(int position) {
            return mList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (convertView==null){
                holder=new ViewHolder();
                convertView=View.inflate(mActivity,R.layout.workerflow_worker_item,null);

                holder.department= (TextView) convertView.findViewById(R.id.tv_department);
                holder.reason= (TextView) convertView.findViewById(R.id.tv_reason);
                holder.time= (TextView) convertView.findViewById(R.id.tv_time);
                holder.state= (TextView) convertView.findViewById(R.id.tv_state);
                convertView.setTag(holder);
            }else {
                holder= (ViewHolder) convertView.getTag();
            }

            holder.department.setText(mList.get(position).getDepartment());
            holder.reason.setText(mList.get(position).content);
            holder.time.setText(mList.get(position).apply_time);
            holder.state.setText(mList.get(position).status);

            return convertView;
        }
    }

    class ViewHolder{
        public TextView department;
        public TextView reason;
        public TextView time;
        public TextView state;
    }

    @Override
    public void onClick(View v) {
        //跳转到添加申请页面
//        Toast.makeText(mActivity, "添加", Toast.LENGTH_SHORT).show();
        mActivity.startActivity(new Intent(mActivity, AddApplyActivity.class));
    }
}
